//
//  HDTrafficListVC.m
//  HDWuDaDaoServiceCenter
//
//  Created by HDNiuKuiming on 2017/4/10.
//  Copyright © 2017年 HDNiuKuiming. All rights reserved.
//

@interface HDRouteSubObj : NSObject
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *desc;

+ (instancetype)subObjWithDict:(NSDictionary *)dict;
@end

@implementation HDRouteSubObj

+ (instancetype)subObjWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}
- (instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict]; // 字典的快速赋值，需要特殊处理一下保留字段和未声明但是字典中有的字段
    }
    return self;
}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    if([key isEqualToString:@"travel_info"]) {
    }
    if([key isEqualToString:@"username"]) {
    }
}

- (void)dealloc
{
    _title = nil;
    _desc = nil;
}

@end

@interface HDRouteObj : NSObject
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSMutableArray *travel_infoArray;


+ (instancetype)routeWithDict:(NSDictionary *)dict;
@end

@implementation HDRouteObj

+ (instancetype)routeWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}

- (instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]) {
        _title = dict[@"title"];
        NSArray *infoArray = dict[@"travel_info"];
        
        [self arrayClean];
        _travel_infoArray = [NSMutableArray new];
        for (NSInteger i=0;i<infoArray.count;i++) {
            NSDictionary *subDic = infoArray[i];
            HDRouteSubObj *subObj = [HDRouteSubObj subObjWithDict:subDic];
            [_travel_infoArray addObject:subObj];
        }
    }
    return self;
}

- (void)arrayClean
{
    [_travel_infoArray removeAllObjects];
    self.travel_infoArray = nil;
}

- (void)dealloc
{
    _title = nil;
    [self arrayClean];
}

@end
#import "HDTrafficListVC.h"

@interface HDTrafficListVC ()<UITableViewDataSource, UITableViewDelegate>
{
    HDDeclare *declare;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *routeArray;
@property (nonatomic, strong) NSMutableSet *pathSet;

@end

@implementation HDTrafficListVC

#pragma mark
#pragma mark -------- Init Methods --------

- (void)viewDidLoad {
    [super viewDidLoad];
    _routeArray = [NSMutableArray new];
    _pathSet = [NSMutableSet new];
    
    [self tableviewInit];
    [self trafficRequestAction];
}

- (void)trafficRequestAction {
    
    HDRequestObj *loginObj = [[HDRequestObj alloc] init];
    NSString *urlPath = [NSString stringWithFormat:@"%@g=mapi&m=Contentinfo&a=travel_info", HTTP_Request_URL_Prefix];
    NSDictionary *parameters = @{@"language": @"CHINESE"};
    
    [loginObj requestGetPath:urlPath Parameter:parameters withVC:self timeout:5];
    loginObj.myDataBlock = ^(NSDictionary *dataDic){
        
        NSString *loginCode = dataDic[@"status"];
        [_routeArray removeAllObjects];

        if (loginCode.integerValue == 1) {
            NSArray *objArray = dataDic[@"data"];
            for (NSInteger i=0;i<objArray.count;i++) {
                NSDictionary *dicTemp = objArray[i];
                HDRouteObj *obj = [HDRouteObj routeWithDict:dicTemp];
                [_routeArray addObject:obj];
            }
            [_tableView reloadData];
        }
        else {
            [LCCoolHUD showFailure:declare.loginFailed zoom:NO shadow:NO];
        }
    };
}

#pragma mark
#pragma mark -------- TableView Delegagte --------

- (void)tableviewInit {
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 44.0f;
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    _tableView.separatorColor = COLOR(142,81,53,1);
    _tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"traffic_bg"]];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _routeArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{ return 60.0f; }

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([_pathSet containsObject:indexPath]) {
        HDRouteObj *obj = _routeArray[indexPath.section];
        HDRouteSubObj *subObj = obj.travel_infoArray[indexPath.row];

        NSString *strDetail = subObj.desc;
        CGFloat textSubH = [self changeStationWith:strDetail anWidthTxtt:tableView.frame.size.width-20 anfont:14.0f]+30;
        
        return MAX(textSubH, 44.0f);
    }
    
    return 44.0f;
}



-(CGFloat)changeStationWith:(NSString *)string anWidthTxtt:(CGFloat)widthText anfont:(CGFloat)fontSize{
    UIFont * tfont = [UIFont systemFontOfSize:fontSize];
    //高度估计文本大概要显示几行，宽度根据需求自己定义。 MAXFLOAT 可以算出具体要多高
    CGSize size =CGSizeMake(widthText,CGFLOAT_MAX);
    //    获取当前文本的属性
    NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil];
    //ios7方法，获取文本需要的size，限制宽度
    CGSize  actualsize =[string boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin  attributes:tdic context:nil].size;
    return actualsize.height;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HDRouteObj *obj = _routeArray[section];
    NSString *title = obj.title;
    UIView *headerBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 60)];
    headerBg.backgroundColor = [UIColor clearColor];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(2, 1, tableView.frame.size.width-4, 57)];
    label.text = title;
    label.numberOfLines = 2.0f;
    label.font = [UIFont systemFontOfSize:15.0f];
    label.backgroundColor = COLOR(253,248,235,1);
    [headerBg addSubview:label];
    
    UIImageView *lineView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 58, tableView.frame.size.width, 2)];
    lineView.image = [UIImage imageNamed:@"header_seg_line"];
    [headerBg addSubview:lineView];
    
    return headerBg;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    HDRouteObj *obj = _routeArray[section];
    return obj.travel_infoArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"trafficListCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    HDRouteObj *obj = _routeArray[indexPath.section];
    HDRouteSubObj *subObj = obj.travel_infoArray[indexPath.row];
    cell.textLabel.text = subObj.title;
    
    cell.textLabel.font = [UIFont systemFontOfSize:15.0f];
    cell.textLabel.numberOfLines = 0;
    
    UIImageView *segImgView = [[UIImageView alloc] initWithFrame:CGRectMake(tableView.frame.size.width - 20 -5, 10, 15, 8)];
    segImgView.image = [UIImage imageNamed:@"cell_down_arrow"];
    cell.detailTextLabel.text = nil;
    if ([_pathSet containsObject:indexPath]) {
        segImgView.image = [UIImage imageNamed:@"cell_up_arrow"];
        cell.detailTextLabel.text = subObj.desc;
    }

    segImgView.contentMode = UIViewContentModeScaleAspectFit;
    cell.accessoryView = segImgView;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ([_pathSet containsObject:indexPath]) {
        [_pathSet removeObject:indexPath];
    }
    else {
        [_pathSet addObject:indexPath];
    }
    
    [tableView reloadData];
}

#pragma mark
#pragma mark -------- Dealloc Methods --------

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)backAction:(id)sender {
    [super back];
}

- (void)dealloc
{
    [_routeArray removeAllObjects];
    _routeArray = nil;
    
    [_pathSet removeAllObjects];
    _pathSet = nil;
    
    declare = nil;
}

@end
